package org.example.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Demo1 {

    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(80);
        System.out.println("服务器启动, 监听80");
        while (true) {
            Socket socket = server.accept();
            InputStream in = socket.getInputStream();
            byte[] buffer = new byte[4096*10];
            int len = in.read(buffer);
            System.out.println(new String(buffer, 0, len));

            OutputStream out = socket.getOutputStream();
            out.write("HTTP/1.1 200 OK\r\n".getBytes());
            out.write("Content-Type: text/html;charset=utf-8\r\n".getBytes());
            out.write("\r\n".getBytes());
            out.write("<h1>hello world</h1>".getBytes());
            out.flush();
            socket.close();
        }
    }

}
